<h2>Objetos de Control de Acceso</h2>
<?php
    function extractIcon($t){
        $i = null;
        if (strpos($t,'[') < strpos($t,']')){
            $i = substr($t, strpos($t,'[') + 1, strpos($t,']') - 1);
            $t = substr($t, strpos($t,']') + 1);
        }
        return $i;
    }
    function extractTitulo($t){
        $i = null;
        if (strpos($t,'[') < strpos($t,']')){
            $i = substr($t, strpos($t,'[') + 1, strpos($t,']') - 1);
            $t = substr($t, strpos($t,']') + 1);
        }
        return $t;
    }
?>
<div id="busqueda">
	<?php
		echo $form->create(null, array('action'=>'index', 'type'=>'post'));
		echo $form->text('q', array('class' => 'fullwidth'));
		echo $form->submit('Enviar', array('div' => false, 'class' => 'btnfind'));
		echo $form->end();
	?>
</div>
<div align="left" style="padding:1px 2px 1px 2px;"><?php $session->flash(); ?></div>
<?php echo $form->create(null, array('action'=>'process', 'type'=>'post'));?>
    <div id="resultados" class="aco index">
        <table border="0" cellpadding="3" cellspacing="0">
            <tr>
                <th width="20px">&nbsp;</th>
                <th width="200px">Alias</th>
                <th>Autorizados</th>
                <th>Orden</th>
                <th>Grupo</th>
                <th>Titulo</th>
                <th>C</th>
                <th width="50px" class="actions">&nbsp;</th>
            </tr>
            <?php
                $i = 0;
                $parentId = 0;
                $subparentId = 0;
                $childIdValue = '';
                foreach ($acos as $aco):
                        $altrow = $i++%2==0?'altrow':'';
                        if (empty($aco['Aco']['parent_id'])){
                                $parentId = $aco['Aco']['id'];
                                $parentIdValue = 'row'.$parentId;
                                $inside = " class='parent {$altrow}' id='{$parentIdValue}'";
                                $tab = '';
                        } else {
                                if ($aco['Aco']['parent_id'] == $parentId //||
//                                        ($aco['Aco']['parent_id'] == $subparentId && $aco['Aco']['alias'] != low($aco['Aco']['alias']))
                                        ){
                                    $childId = $aco['Aco']['id'];
                                    $subparentId = $aco['Aco']['id'];
                                    $childIdValue = 'row'.$childId;
                                    $inside = " class='parent child-{$parentIdValue}' id='{$childIdValue}' style='color:blue;'";
                                    $tab='&nbsp;&nbsp;&nbsp;&nbsp;';
                                } else {
                                    $inside = " class='child-{$childIdValue}' style='color:green;'";
                                    $tab = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                                }
                        }
            ?>
            <tr <?php echo $inside;?>>
                <td width="5px" class="notclick">
                        <?php echo $form->checkbox('id',array(
                                'div'=>false,
                                'label'=>false,
                                'name'=>"data[$i][id]",
                                'value'=>$aco['Aco']['id'])); ?>
                </td>
                <td class="alias">
                        <?php echo $tab.$aco['Aco']['alias'];?>
                </td>
                <td>
                <?php
                        foreach($aco['Aro'] as $aro){

                                echo '['.$aro['model'].'.'.$aro['foreign_key'].']';
                        }
                ?>
                </td>
                <td>
                        <?php echo $tab.$aco['Aco']['orden'];?>
                </td>
                <td>
                        <?php
                        $title = extractTitulo($aco['Aco']['grupo']);
                        $icon = extractIcon($aco['Aco']['grupo']);
                        $icon = !empty($icon)?$html->image($icon, array('border'=>0)):'';
                        echo sprintf('%s %s %s', $tab, $icon, $title);
                        ?>
                </td>
                <td>
                        <?php
                        $title = extractTitulo($aco['Aco']['titulo']);
                        $icon = extractIcon($aco['Aco']['titulo']);
                        $icon = !empty($icon)?$html->image($icon, array('border'=>0)):'';
                        echo sprintf('%s %s %s', $tab, $icon, $title);
                        ?>
                </td>
                <td>
                        <?php echo $aco['Aco']['con_cierre']?'SI':'';?>
                </td>
                <td width="50px" align="center" class="notclick">
                        <?php echo $html->link(__('Editar', true), array('action' => 'edit', $aco['Aco']['id']), array('class'=>'lnkbutton btnedit')); ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </table>
    </div>
    <div class="actions">
        <?php echo $html->link('Volver', array('controller'=>'Home','action'=>'index'), array('class'=>'lnkbutton btnback'));?>
        <?php echo $html->link('Nuevo', array('action'=>'add'), array('class'=>'lnkbutton btnnew'));?>
        <?php echo $form->submit('Eliminar', array(
            'div' => false,
            'class' => 'btnerase',
            'value'=>true,
            'name'=>'delete'));?>
    </div>
<?php echo $form->end();?>
<img src="img/expand_collapse_plus.gif" id="plus" style="display: none;" border="0" alt="Expandir" />
<img src="img/expand_collapse_minus.gif" id="minus" style="display: none;" border="0" alt="Contraer" />
<p/>
<script type="text/javascript">
    $(function(){
        $('#AreaQ').focus();
        $('tr[class*=child-]').hide().children('td');
        $('tr.parent td.alias').prepend('<?php echo $html->image('expand_collapse_plus.gif', array('id'=>'plus', 'border'=>0, 'alt'=>'Expander', 'style'=>'display: inline;'));?>');

        $('tr.parent td:not(.notclick)')
            .css("cursor","pointer")
            .attr("title","Click para expandir/contrer")
            .click(function(){
                var parent = $(this).parent();
                $('.child-'+parent.attr('id')).toggle();
                if ($('.child-'+parent.attr('id')).css('display') != 'none'){
                    parent.children('td.alias').children('#plus').remove();
                    parent.children('td.alias').prepend('<?php echo $html->image('expand_collapse_minus.gif', array('id'=>'minus', 'border'=>0, 'alt'=>'Contraer', 'style'=>'display: inline;'));?>');
                    $('.child-'+parent.attr('id')).each(function(){
                        $(this).click();
                    });
                } else {
                    parent.children('td.alias').children('#minus').remove();
                    parent.children('td.alias').prepend('<?php echo $html->image('expand_collapse_plus.gif', array('id'=>'plus', 'border'=>0, 'alt'=>'Expandir', 'style'=>'display: inline;'));?>');
                }
                parent.siblings('.parent.child-'+parent.attr('id')).each(function(){
                    if ($(this).css('display') == 'none'){
                        var childId = $(this).attr('id');
                        if ($(this).siblings('.child-'+childId).css('display') != 'none'){
                            $(this).children('td.alias').children('#minus').remove();
                            $(this).children('td.alias').prepend('<?php echo $html->image('expand_collapse_plus.gif', array('id'=>'plus', 'border'=>0, 'alt'=>'Expandir', 'style'=>'display: inline;'));?>');
                            $(this).siblings('.child-'+childId).hide();
                            $('.child-'+this.id).each(function(){
                                    $(this).click();
                            });
                        }
                    }
                });
            });
    });
</script>
<pre>
<?php //print_r($acos);?>
</pre>